import xml.parsers.expat
from UserString import MutableString


class EmailTemplatesEnumGen:
    def __init__(self):
        #print 'a'
        self.parser = xml.parsers.expat.ParserCreate()
        self.parser.StartElementHandler = self.start
        self.parser.EndElementHandler = self.end
        self.parser.CharacterDataHandler = self.data
        self.templateName = False
        self.tag = False
        self.defaultRecipient = False
        self.subject = False
        self.message = False
        self.tags =[]
        self.messageValue = MutableString()
        
    def start(self,name,attrs):
        #print "##"+name
        if name =='Template':
            print attrs["id"]+":",
            self.templateId = attrs["id"]
            self.tags=[]
            self.messageValue =MutableString()
        elif name == 'TemplateName':
            self.templateName = True
        elif name == 'DefaultRecipient':
            self.defaultRecipient = True
        elif name == 'tag':
            self.tag = True
        elif name == 'subject':
            self.subject = True
        elif name == 'message':
            self.message = True
            
            
    def end(self,name):
        if name =='Template':
            self.printTemplateEnum()
            self.printTemplateSQL1()
            self.printTemplateSQL2()
            self.printUIEntry()
        elif name =='TemplateName':
            self.templateName = False
        elif name == 'DefaultRecipient':
            self.defaultRecipient = False
        elif name == 'tag':
            self.tag = False
        elif name == 'subject':
            self.subject = False
        elif name == 'message':
            self.message = False           
            
    def data(self,data):
        if self.templateName:
            self.templateNameValue = self.processValue(data)      
            self.templateNameValueForDisplay = data     
            print self.templateNameValue
        elif self.defaultRecipient:
            self.defaultRecipientValue = self.processValue(data)
        elif self.tag:
            self.tags.append("\t\tEmailTags."+self.processValue(data))
        elif self.subject:
            self.subjectValue = data
        elif self.message:
                if (data.strip()!=""):
                    self.messageValue+="&lt;p>" + data.strip() + "&lt;/p>"
        
    def feed(self,data):
        
        self.outputEnum = open('outputEnum', 'w')
        self.outputSQL1 = open('outputSQL1', 'w') 
        self.outputSQL2 = open('outputSQL2', 'w') 
        self.outputUIEntries = open('UIEntries', 'w')
        self.outputSQL1.write("""<table name="er_notification">\n""")
        self.outputSQL2.write("""<table name="er_notification_text">\n""")
        
        
        self.parser.Parse(data,0)
        
        
        self.outputSQL1.write("""</table>\n""")
        self.outputSQL2.write("""</table>\n""")
        
        self.outputEnum.close()
        self.outputSQL1.close()
        self.outputSQL2.close()
        self.outputUIEntries.close()
        
    def printUIEntry(self):
        
        out_string = MutableString()
        out_string += """<entry key="RECRUITMENT_EMAILTEMPLATES_%s">\n""" %(self.templateNameValue)
        out_string += """\t<default lang="language">%s</default>\n""" %(self.templateNameValueForDisplay)
        out_string += "\t<keylabel>%s email template</keylabel>\n"%(self.templateNameValueForDisplay)
        out_string += "\t<module>Recruitment</module>\n"
        out_string += "\t<area>Email Templates</area>\n"
        out_string += "</entry>\n"
        
        self.outputUIEntries.write(out_string.data)
        
    def printTemplateEnum(self):
        
        out_str = MutableString()
     
        out_str +=self.templateNameValue+"("+self.templateId+",\n"
        out_str += "\tEnumSet.of(\n"
        
        out_str +=",\n".join(self.tags)
            
        out_str += "\t),\n"
        out_str += "\tEnumSet.of(\n"
        out_str += "\t\tRoles.%s\n" % (self.defaultRecipientValue)
        out_str += "\t)\n"
        out_str += "),\n"
        
        self.outputEnum.write(out_str.data)
    
    def printTemplateSQL1(self):
        
        out_str = MutableString()
        out_str += "<defaultData>insert into ER_NOTIFICATION (NOTIFICATION_ID,APPLICATION_TASK_ID,UIKEY,IS_ARCHIVED) values (%s,null,'%s','Y')</defaultData>\n" % (self.templateId,self.templateNameValue)
        
        
        self.outputSQL1.write(out_str.data)
        
    def printTemplateSQL2(self):
        
        out_str = MutableString()
        out_str += "<defaultData>Insert into ER_NOTIFICATION_TEXT (NOTIFICATION_ID,LANGUAGE,SUBJECT,BODY) values (%s,'language','%s','%s')</defaultData>\n" % (self.templateId,self.subjectValue,self.messageValue.data)
        
        
        self.outputSQL2.write(out_str.data)        
        
    def processValue(self,data):
        return data.upper().strip().replace(" ","_").replace("(","_").replace(")","_").replace("/","_")
        
if(__name__=='__main__'):
    print 'd1---------->:'
    enumGen = EmailTemplatesEnumGen()
    file_object = open('EmailTemplates.xml','r')
    try:
        data = file_object.read()
        enumGen.feed(data)
    finally:
        file_object.close()
        
    
               
    